
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.GoodsQrcodeDao;
import com.sun.showcase.biz.mall.service.GoodsQrcodeService;
import com.sun.showcase.client.domain.mall.GoodsQrcode;
import com.sun.showcase.client.query.mall.GoodsQrcodeQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("goodsQrcodeService")
@Transactional
public class GoodsQrcodeServiceImpl implements GoodsQrcodeService{
	@Autowired
	private GoodsQrcodeDao goodsQrcodeDao;
	
	public void setGoodsQrcodeDao(GoodsQrcodeDao dao) {
		this.goodsQrcodeDao = dao;
	}

	public DataGrid datagrid(GoodsQrcodeQuery goodsQrcodeQuery) {
		DataGrid j = new DataGrid();
		Pager<GoodsQrcode> pager  = find(goodsQrcodeQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<GoodsQrcodeQuery> getQuerysFromEntitys(List<GoodsQrcode> goodsQrcodes) {
		List<GoodsQrcodeQuery> goodsQrcodeQuerys = new ArrayList<GoodsQrcodeQuery>();
		if (goodsQrcodes != null && goodsQrcodes.size() > 0) {
			for (GoodsQrcode tb : goodsQrcodes) {
				GoodsQrcodeQuery b = new GoodsQrcodeQuery();
				BeanUtils.copyProperties(tb, b);
				goodsQrcodeQuerys.add(b);
			}
		}
		return goodsQrcodeQuerys;
	}

	private Pager<GoodsQrcode> find(GoodsQrcodeQuery goodsQrcodeQuery) {
		Page<GoodsQrcode> page = PageHelper.startPage(goodsQrcodeQuery.getPage().intValue(), goodsQrcodeQuery.getRows().intValue());
		List<GoodsQrcode> list = goodsQrcodeDao.findList(goodsQrcodeQuery);
		Pager<GoodsQrcode> pager = new Pager<GoodsQrcode>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public GoodsQrcode add(GoodsQrcodeQuery goodsQrcodeQuery) {
		GoodsQrcode t = new GoodsQrcode();
		BeanUtils.copyProperties(goodsQrcodeQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		goodsQrcodeDao.save(t);
		goodsQrcodeQuery.setId(t.getId());
		return t;
		
	}

	public void update(GoodsQrcodeQuery goodsQrcodeQuery) {
		GoodsQrcode t = goodsQrcodeDao.getById(goodsQrcodeQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(goodsQrcodeQuery, t);
	    	goodsQrcodeDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(GoodsQrcodeQuery goodsQrcodeQuery) {
	    goodsQrcodeDao.updatePart(goodsQrcodeQuery);
	}

	public void delete(String[] ids) {
		goodsQrcodeDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		goodsQrcodeDao.deletePt(ids);
	}

	public GoodsQrcode get(GoodsQrcodeQuery goodsQrcodeQuery) {
		return goodsQrcodeDao.getById(goodsQrcodeQuery.getId());
	}

	public GoodsQrcode get(String id) {
		return goodsQrcodeDao.getById(id);
	}

	
	public List<GoodsQrcodeQuery> listAll(GoodsQrcodeQuery goodsQrcodeQuery) {
	    List<GoodsQrcode> list = goodsQrcodeDao.findList(goodsQrcodeQuery);
		List<GoodsQrcodeQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public GoodsQrcode getByGoodsAndUserId(String goodsId, String userId) {
		return goodsQrcodeDao.getByGoodsAndUserId(goodsId, userId);
	}
	
	
}
